package ar.edu.utn.frre.sistop.simumem;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;
import ar.edu.utn.frre.sistop.simumem.common.Preferences;
import ar.edu.utn.frre.sistop.simumem.exceptions.NoMemoryException;
import ar.edu.utn.frre.sistop.simumem.exceptions.NoProcessesException;
import ar.edu.utn.frre.sistop.simumem.exceptions.ProcessSizeTooBigException;
import ar.edu.utn.frre.sistop.simumem.fragments.FixedPartitionParameterLoadFragment;
import ar.edu.utn.frre.sistop.simumem.fragments.PartitionLoadFragment;
import ar.edu.utn.frre.sistop.simumem.fragments.ProcessLoadFragment;
import ar.edu.utn.frre.sistop.simumem.logic.Simulador;
import ar.edu.utn.frre.sistop.simumem.model.Memoria;

public class DataLoadActivity extends ActionBarActivity implements OnPageChangeListener {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
	 * sections. We use a {@link FragmentPagerAdapter} derivative, which will keep every loaded
	 * fragment in memory. If this becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	private SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	private ViewPager mViewPager;

	private ProcessLoadFragment mProcessLoadFragment;
	private PartitionLoadFragment mPartitionLoadFragment;
	private FixedPartitionParameterLoadFragment mFixedPartitionParameterLoadFragment;

	static final String PARTITION_TYPE_VARIABLE_EXTRA = "partition-type";
	private static final int PAGE_PROCESS = 0;
	private static final int PAGE_PARTITION = 1;
	// private static final int PAGE_PARAMETER_FIXED_PARTITION = 2;
	
	private Preferences mPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setPreferences(new Preferences(this));
		
		setContentView(R.layout.activity_data_load);

		// Create the adapter that will return a fragment for each of the two
		// primary sections of the activity.
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager_data_load);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setOffscreenPageLimit(2);
	}

	@Override
	protected void onResumeFragments() {
		super.onResumeFragments();
		switch (mViewPager.getCurrentItem()) {
		case PAGE_PROCESS:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Procesos);
			break;
		case PAGE_PARTITION:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Particiones);
			break;
		default:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Particiones_Fijas_Parametros);
			break;
		}
		Toast.makeText(this, R.string.toastDeleteRowInfo, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.data_load, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_confirm:
			try {
				Simulador.validarTamanoDeProcesos(mProcessLoadFragment.getProcesos(), new Memoria(
						mPartitionLoadFragment.getParticiones()));
			} catch (NoProcessesException e) {
				mViewPager.setCurrentItem(PAGE_PROCESS);
				Toast.makeText(this, R.string.exceptionNoProcesses, Toast.LENGTH_SHORT).show();
				return false;
			} catch (NoMemoryException e) {
				mViewPager.setCurrentItem(PAGE_PARTITION);
				Toast.makeText(this, R.string.exceptionNoMemory, Toast.LENGTH_SHORT).show();
				return false;
			} catch (ProcessSizeTooBigException e) {
				mViewPager.setCurrentItem(PAGE_PROCESS);
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
				return false;
			}
			Intent results = new Intent(this, ResultsActivity.class);
			startActivity(results);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the
	 * sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			switch (position) {
			case PAGE_PROCESS:
				mProcessLoadFragment = new ProcessLoadFragment();
				return mProcessLoadFragment;
			case PAGE_PARTITION:
				mPartitionLoadFragment = new PartitionLoadFragment();
				return mPartitionLoadFragment;
			default:
				mFixedPartitionParameterLoadFragment = new FixedPartitionParameterLoadFragment();
				return mFixedPartitionParameterLoadFragment;
			}
		}

		@Override
		public int getCount() {
			return 3;
		}
	}

	@Override
	public void onPageSelected(int position) {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		switch (position) {
		case PAGE_PROCESS:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Procesos);
			findViewById(R.id.txtDataLoadProcess_id).requestFocus();
			break;
		case PAGE_PARTITION:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Particiones);
			findViewById(R.id.txtDataLoadPartition_size).requestFocus();
			break;
		default:
			getSupportActionBar().setTitle(R.string.titleDataLoad_Particiones_Fijas_Parametros);
			break;
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}

	public Preferences getPreferences() {
		return mPreferences;
	}

	public void setPreferences(Preferences mPreferences) {
		this.mPreferences = mPreferences;
	}
}
